﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data;
using RapChieuPhim.DTO;
using System.Data.SqlClient;

namespace RapChieuPhim.DAO
{
    class RapDAO
    {
        public RapDTO GetByID(int id)
        {
            DataProvider cn = new DataProvider();
            SqlParameter param = new SqlParameter("@MaRap", id);
            DataTable dt = cn.SqlExcuteQuery("LayRapTheoID", param);
            RapDTO Rap = new RapDTO();

            if (dt.Rows.Count != 0)
            {
                Rap.MaRap = int.Parse(dt.Rows[0]["MaRap"].ToString());
                Rap.TenRap= dt.Rows[0]["TenRap"].ToString();
                Rap.DiaChi = dt.Rows[0]["DiaChi"].ToString();
            }
            return Rap;
        }

        public IList GetAll()
        {
            DataProvider cn = new DataProvider();
            ArrayList array = new ArrayList();
            DataTable dt = cn.SqlExcuteQuery("LayAllRap");

            if (dt.Rows.Count != 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    RapDTO Rap = new RapDTO();
                    Rap.MaRap = int.Parse(dr["MaRap"].ToString());
                    Rap.TenRap = dr["TenRap"].ToString();
                    Rap.DiaChi = dr["DiaChi"].ToString();
                    array.Add(Rap);
                }
            }
            return array;
        }

        public int LayLastRap()
        {
            DataProvider cn = new DataProvider();
            DataTable dt = cn.SqlExcuteQuery("LayLastRap");
            int _LastID = int.Parse(dt.Rows[0][0].ToString());
            return _LastID;
        }

        public IList GetAllbyMaRap(int _MaRap)
        {
            DataProvider cn = new DataProvider();
            ArrayList array = new ArrayList();
            SqlParameter param = new SqlParameter("@MaRap", _MaRap);
            DataTable dt = cn.SqlExcuteQuery("LayAllRapbyMaRap", param);

            if (dt.Rows.Count != 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    RapDTO Rap = new RapDTO();
                    Rap.MaRap = int.Parse(dr["MaRap"].ToString());
                    Rap.TenRap = dr["TenRap"].ToString();
                    Rap.DiaChi = dr["DiaChi"].ToString();
                    array.Add(Rap);
                }
            }
            return array;
        }

        public void Insert(RapDTO Rap)
        {
            DataProvider cn = new DataProvider();
            SqlParameter[] param = new SqlParameter[3];
            param[0] = new SqlParameter("@MaRap", Rap.MaRap);
            param[1] = new SqlParameter("@TenRap", Rap.TenRap);
            param[2] = new SqlParameter("@DiaChi", Rap.DiaChi);
            cn.SqlExcuteNonQuery("InsertRap", param);
        }

        public void Update(RapDTO Rap)
        {
            DataProvider cn = new DataProvider();
            SqlParameter[] param = new SqlParameter[3];
            param[0] = new SqlParameter("@MaRap", Rap.MaRap);
            param[1] = new SqlParameter("@TenRap", Rap.TenRap);
            param[2] = new SqlParameter("@DiaChi", Rap.DiaChi);
            cn.SqlExcuteNonQuery("UpdateRap", param);
        }

        public void Delete(int id)
        {
            DataProvider cn = new DataProvider();
            SqlParameter param = new SqlParameter("@MaRap", id);
            cn.SqlExcuteNonQuery("DeleteRap", param);
        }
    }
}
